/*
 * Copyright 2002-present the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.integration.handler.advice;

import org.jspecify.annotations.Nullable;

import org.springframework.core.AttributeAccessor;
import org.springframework.integration.core.ErrorMessagePublisher;
import org.springframework.integration.core.RecoveryCallback;
import org.springframework.integration.support.DefaultErrorMessageStrategy;
import org.springframework.integration.support.ErrorMessageStrategy;
import org.springframework.messaging.MessageChannel;

/**
 * A {@link RecoveryCallback} that sends the final throwable as an
 * {@link org.springframework.messaging.support.ErrorMessage} after
 * retry exhaustion.
 *
 * @author Gary Russell
 * @author Artem Bilan
 * @author Oleg Zhurakousky
 *
 * @since 2.2
 *
 */
public class ErrorMessageSendingRecoverer extends ErrorMessagePublisher implements RecoveryCallback<@Nullable Object> {

	/**
	 * Construct instance with the default {@code errorChannel}
	 * to publish a recovery error message.
	 * The {@link DefaultErrorMessageStrategy} is used for building an error message to publish.
	 * @since 4.3.10
	 */
	public ErrorMessageSendingRecoverer() {
	}

	/**
	 * Construct an instance based on the provided message channel.
	 * The {@link DefaultErrorMessageStrategy} is used for building an error message to publish.
	 * @param channel the message channel to publish error messages on recovery action.
	 */
	@SuppressWarnings("this-escape")
	public ErrorMessageSendingRecoverer(MessageChannel channel) {
		setChannel(channel);
	}

	/**
	 * Construct instance based on the provided message channel and {@link ErrorMessageStrategy}.
	 * In the event provided {@link ErrorMessageStrategy} is null, the {@link DefaultErrorMessageStrategy}
	 * will be used.
	 * @param channel the message channel to publish error messages on recovery action.
	 * @param errorMessageStrategy the {@link ErrorMessageStrategy}
	 * to build error message for publishing. Can be null at which point the
	 * {@link DefaultErrorMessageStrategy} is used.
	 * @since 4.3.10
	 */
	@SuppressWarnings("this-escape")
	public ErrorMessageSendingRecoverer(MessageChannel channel, ErrorMessageStrategy errorMessageStrategy) {
		setChannel(channel);
		setErrorMessageStrategy(errorMessageStrategy);
	}

	@Override
	public @Nullable Object recover(AttributeAccessor context, Throwable cause) {
		publish(cause, context);
		return null;
	}

}
